import { h, ref, unref, reactive } from 'vue'
import { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { findDictTypeList} from '@/api/common'
import tabTag from '../components/tabTag.vue'
import tabButton from '../components/tabButton.vue'
import { dataToLabel,dataToSelect} from '@/utils'
import {  usableStatus,associationStatus} from '@/utils/dict'
let router:any = null
/**
 *  获取路由对象
 */
export function getRouterInstance(r) {
  router = r
}

let DT:any = null

// 获取data对象
export function getDataInstance(data) {
  DT = data
}


const ids = ref<string[]>([])
// 挂载类型
const mountType = ref([])
const mountTypeArr = ref({})
const mountBrandToLabel = ref({})

// 跳转详情 info 详情  edit 编辑
const linkTo = (row: Object, type: string) => {
  router.push({ path: '/resource-manage/mount_info', query: { type,id:row.id } })
}


//  挂载类型
export const setMountType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'mount'}) as any
  const mount_type = data.find(item=> item.dictType==='mount_type').dateList ||[]
  const mount_brand = data.find(item=> item.dictType==='mount_brand').dateList ||[]
  mountType.value = dataToSelect(mount_type, { label: 'dictLabel', value: 'dictValue' }) 
  mountTypeArr.value = dataToLabel(mount_type,'dictValue','dictLabel') 
  mountBrandToLabel.value = dataToLabel(mount_brand,'dictValue','dictLabel') 
}

// 列表页对象
export default reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    search: {
      hidden: true
    },
  },
  {
    field: 'code',
    label: '挂载编号',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.code
        }
      }
    },
  },
  {
    field: 'type',
    label: '挂载类型',
    table: {
      slots: {
        default: (data: any) => {
          return  mountTypeArr.value[data.row.type]
        }
      }
    },
    search:{
      component: 'Select',
      componentProps: {
        options:mountType
      },
    }
  },
  {
    field: 'brand',
    label: '品牌',
    table: {
      slots: {
        default: (data: any) => {
          return  mountBrandToLabel.value[ data.row.brand]
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'series',
    label: '设备型号',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.series
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'status',
    width: '120px',
    label: '可用状态',
    table: {
      slots: {
        default: (data: any) => {
          const status = data.row.status
          return h(tabTag, {
            type: status,
            tags:usableStatus
          })
        }
      }
    }, 
    search:{
      component: 'Select',
      componentProps: {
        options:usableStatus
      },
    }
  },
  {
    field: 'deviceStatus',
    width: '120px',
    label: '关联状态',
    table: {
      slots: {
        default: (data: any) => {
          const status = data.row.status
          return h(tabTag, {
            type: status,
            tags:associationStatus
          })
        }
      }
    },
    search:{
      hidden: true
    }
  },
  {
    field: 'factoryCode',
    label: '原厂编号',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.factoryCode
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'droneList',
    label: '关联无人机',
    table: {
      slots: {
        default: (data: any) => {
          const droneList =  data.row.droneList ||[]
          return droneList.map(item=>{return item.name}).join('、')
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'createUserName',
    label: '创建人',
    table: {
      slots: {
        default: (data: any) => {
          return data.row.createUserName
        }
      }
    },
    search: {
      hidden: true
    },
  },
  {
    field: 'action',
    width: '280px',
    label: '操作',
    search: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return h(tabButton, {
            actions: [
              {
                type: 'button',
                tip: '详情',
                props: {
                  type: 'success',
                  onClick: () => linkTo(data.row, 'detail')
                }
              },
              {
                type: 'button',
                tip: '编辑',
                props: {
                  type: 'primary',
                  onClick: () => linkTo(data.row, 'edit')
                }
              },
              {
                type: 'button',
                tip: '删除',
                props: {
                  type: 'danger',
                  onClick: () => DT.delData(data.row)
                }
              }
            ]
          })
        }
      }
    }
  }
])




